<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>私有属性和方法的封装</title>
</head>
<body>
    <script>
        function Book(title,author,year){
            // 对内 私有
            // 对外
            let _title = title; // _开始的变量 私有变量 内部 有利于可读性的编程风格
            let _author = author;
            let _year = year;
            this.getTitle = function(){
                return _title
            }
            // 私有
            function getFullTitle() {
                return `${_title} by ${_author}`
            }
            this.getFullInfo = function() {
                return `${getFullTitle()} published in ${_year}`
            }
            this.getAuthor = function() {
                return _author
            }
            this.getYear = function() {
                return _year
            }
            // 类的开发者和类的使用者，可能是两拨人
            this.updateYear = function(newYear) {
                if(typeof newYear === 'number' && newYear > 0){
                    _year = newYear
                }else{
                    console.log('invalid year')
                }
            }
        }

        let book = new Book("三国演义","罗贯中",1999)
        // console.log(book._title)
        console.log(book.getTitle())
        console.log(book.getFullInfo())
        // book._yaer = '2025';
        book.updateYear(2025);
        console.log(book.getYear())
    </script>
</body>
</html>